<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>JSDoc: Source: base/mixin/users.js</title>

    <script src="scripts/prettify/prettify.js"> </script>
    <script src="scripts/prettify/lang-css.js"> </script>
    <!--[if lt IE 9]>
      <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
    <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>

<body>

<div id="main">

    <h1 class="page-title">Source: base/mixin/users.js</h1>

    



    
    <section>
        <article>
            <pre class="prettyprint source linenums"><code>/**
 *  users mixin module
 *  @module base/mixin/users
 *  @author coder
 */
import {
  fetchUsers,
  getUsers,
  addUsers,
  updateUsers,
  removeUsers,
  batchUsers
} from '@/base/api/users'

// mixin
export default {

  /**
   * users数据存储载体对象
   * @member users
   * @property {Array} [list=[]] 获取users列表响应数据
   * @property {number} [page=null] 获取users列表响应页码
   * @property {number} [limit=null] 获取users列表页大小，每页记录条数
   * @property {number} [total=0] 获取users列表数据记录总条数
   * @property {object} [model=null] 获取users单条记录
   */
  data() {
    return {
      users: {
        list: [],
        page: null,
        limit: null,
        total: 0,
        model: null
      }
    }
  },
  methods: {

    /**
     * 获取users列表
     * @function fetchUsers
     * @param {object} data 请求发送的数据data
     * @returns {Promise} Promise实例
     */
    fetchUsers(data) {
      return fetchUsers(data).then(res => {
        this.users.list = res.list || []
        this.users.page = Number.parseInt(res.page) || 0
        this.users.limit = Number.parseInt(res.limit) || 0
        this.users.total = Number.parseInt(res.total) || 0
        return res
      })
    },
    /**
     * 获取users单条记录
     * @function getUsers
     * @param {string|number} id 请求URL路径参数id数据
     * @param {object} data 请求发送的数据data
     * @returns {Promise} Promise实例
     */
    getUsers(id, data) {
      return getUsers(id, data).then(res => {
        this.users.model = Object.assign({}, data, res || {})
        return res
      })
    },
    /**
     * 新增users
     * @function addUsers
     * @param {object} data 请求发送的数据data
     * @param {number} [index] 新增到列表的位置索引，默认最近到列表最后
     * @returns {Promise} Promise实例
     */
    addUsers(data, index) {
      return addUsers(data, index).then(res => {
        this.users.model = Object.assign({}, data, res || {})
        if (index !== undefined) {
          this.users.list.splice(index, 0, this.users.model)
        } else {
          this.users.list.push(this.users.model);
        }
        this.users.total += 1;
        return res
      })
    },
    /**
     * 更新users
     * @function updateUsers
     * @param {object} data 请求发送的数据data
     * @param {number} [index] 源实体在列表的位置索引，如没有index，即不更新实体在源列表数据
     * @returns {Promise} Promise实例
     */
    updateUsers(data, index) {
      return updateUsers(data, index).then(res => {
        this.users.model = Object.assign({}, data, res || {})
        if (index !== undefined) {
          this.users.list.splice(index, 1, this.users.model)
        }
        return res
      })
    },
    /**
     * 删除users
     * @function removeUsers
     * @param {string|number} id 请求URL路径参数id数据
     * @param {object} data 请求发送的数据data
     * @param {number} [index] 源实体在列表的位置索引，如没有index，即不删除实体在源列表数据
     * @returns {Promise} Promise实例
     */
    removeUsers(id, data, index) {
      return removeUsers(id, data).then(res => {
        if (index !== undefined) {
          this.users.list.splice(index, 1)
        }
        this.users.total -= 1;
        return res
      })
    },
    /**
     * 批量删除users
     * @function batchUsers
     * @param {object} data 请求发送的数据data
     * @param {number[]} [indexes] 源实体在列表的位置索引，如没有indexes，即不删除实体在源列表数据
     * @returns {Promise} Promise实例
     */
    batchUsers(data, indexes = []) {
      return batchUsers(data).then(res => {
        if (indexes.length > 0) {
          this.users.list = this.users.list.filter((item, index) => !indexes.includes(index))
        }
        this.users.total -= indexes.length;
        return res
      })
    }
  }
}
</code></pre>
        </article>
    </section>




</div>

<nav>
    <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-base_api_users.html">base/api/users</a></li><li><a href="module-base_mixin_users.html">base/mixin/users</a></li><li><a href="module-base_types.html">base/types</a></li><li><a href="module-mapping_index.html">mapping/index</a></li><li><a href="module-mock_templates.html">mock/templates</a></li><li><a href="module-router_index.html">router/index</a></li><li><a href="module-store_getters.html">store/getters</a></li><li><a href="module-store_index.html">store/index</a></li><li><a href="module-store_mutations.html">store/mutations</a></li><li><a href="module-store_state.html">store/state</a></li></ul>
</nav>

<br class="clear">

<footer>
    Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Tue Apr 09 2019 16:26:55 GMT+0800 (China Standard Time)
</footer>

<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>
